package cn.ly.security.controller;

import cn.ly.security.entity.DeviceUserArea;
import cn.ly.security.entity.Equipment;
import cn.ly.security.entity.Info;
import cn.ly.security.entity.User;
import cn.ly.security.service.impl.DeviceUserAreaServiceImpl;
import cn.ly.security.service.impl.EquipmentServiceImpl;
import cn.ly.security.service.impl.InfoServiceImpl;
import cn.ly.security.util.DataPojo;
import cn.ly.security.util.Pagely;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/DeviceUserArea")
@Api(tags="区域管理")
@CrossOrigin("*")
public class DeviceUserAreaController {
    @Autowired
    DeviceUserAreaServiceImpl deviceUserAreaService;

    @Autowired
    InfoServiceImpl infoService;

    @Autowired
    EquipmentServiceImpl equipmentService;

    @DeleteMapping("/deleteByPrimaryKeys")
    @ApiOperation(value="根据id数组来删除",notes="别传null")
    public DataPojo deleteByPrimaryKeys(Integer[] ids) {
        for (Integer id : ids) {
            DeviceUserArea deviceUserArea = deviceUserAreaService.selectDeviceUserAreaById(id);
            infoService.removeById(deviceUserArea.getDDeviceId());
            deviceUserAreaService.removeById(id);
        }
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(true);
        return  new DataPojo("200","无异常",objects);
    }

    @DeleteMapping("/deleteByPrimaryKey")
    @ApiImplicitParams(
            @ApiImplicitParam(value = "id",name="id",dataType="int",required = true,paramType = "query")
    )
    public DataPojo deleteByPrimaryKey(Integer id) {
        DeviceUserArea deviceUserArea = deviceUserAreaService.selectDeviceUserAreaById(id);
        infoService.removeById(deviceUserArea.getInfo().getIId());
        boolean i = deviceUserAreaService.removeById(id);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(i);
        return  new DataPojo("200","无异常",objects);
    }

    @PutMapping("/insert")
    public DataPojo insert(DeviceUserArea record, Info info, User user) {
        infoService.save(info);
        record.setDDeviceId(info.getIId());
        record.setDUserId(user.getUId());
        boolean i = deviceUserAreaService.save(record);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(i);
        return  new DataPojo("200","无异常",objects);
    }

    @PostMapping("/updateByPrimaryKey")
    public DataPojo updateByPrimaryKey(DeviceUserArea record, Info info, Equipment equipment, User user) {
        info.setIDevicecateId(equipment.getEId());
        infoService.updateById(info);
        record.setDUserId(user.getUId());
        boolean i = deviceUserAreaService.updateById(record);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(i);
        return  new DataPojo("200","无异常",objects);
    }

    @GetMapping("/selectByPrimaryKey")
    public DataPojo selectByPrimaryKey(Integer id) {
        DeviceUserArea deviceUserArea = deviceUserAreaService.selectDeviceUserAreaById(id);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(deviceUserArea);
        return  new DataPojo("200","无异常",objects);
    }

    @GetMapping("/selectByAreaName")
    public DataPojo selectByAreaName(String areaName, Pagely pagely) {
        Page<DeviceUserArea> page = new Page<>(pagely.getCurrent(), pagely.getSize());
        int count = deviceUserAreaService.count();
        List<DeviceUserArea> deviceUserAreas = deviceUserAreaService.selectAllDeviceUserAreaByName(pagely.getCurrent(), pagely.getSize(), areaName);
        page.setRecords(deviceUserAreas);
        page.setTotal(count);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(page);
        return  new DataPojo("200","无异常",objects);
    }

    @GetMapping("/selectAll")
    public DataPojo selectAll(Pagely pagely) {
        Page<DeviceUserArea> userPage = new Page<>(pagely.getCurrent(), pagely.getSize());
        int count = deviceUserAreaService.count();
        List<DeviceUserArea> deviceUserAreas = deviceUserAreaService.selectAllDeviceUserArea(pagely.getCurrent(), pagely.getSize());
        userPage.setRecords(deviceUserAreas);
        userPage.setTotal(count);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(userPage);
        return new DataPojo("200","无异常",objects);
    }
}
